/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo;

import java.io.File;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import org.toedo.controllers.Controller;
import org.toedo.controllers.DataController;
import org.toedo.controllers.events.EventController;
import org.toedo.gui.actions.UnsavedModifiedCheckerAction;
import org.toedo.logging.Log;
import org.toedo.preferences.PreferenceConstants;
import org.toedo.utils.Constants;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	private static final String PERSPECTIVE_ID = "toedo-core.MainPerspective"; //$NON-NLS-1$

    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }

	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}
	
	public void initialize(IWorkbenchConfigurer configurer) {
		Log.getInstance(ApplicationWorkbenchAdvisor.class).info(String.format("Initializing %s.", Constants.APP_NAME)); //$NON-NLS-1$
		
		configurer.setSaveAndRestore(true);
	}
	
	public void postStartup() {
		Log.getInstance(ApplicationWorkbenchAdvisor.class).info(Constants.APP_NAME + " initialized."); //$NON-NLS-1$
		
		if (Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.OPEN_LAST_PROJECT_AT_STARTUP)) {
			String fileName = Controller.getInstance().getPreferenceStore().getString(PreferenceConstants.LAST_OPEN_PROJECT);
			if ((fileName != null) &&
					(!fileName.isEmpty())) {
				File file = new File(fileName);
				if ((file.exists()) &&
						(file.isFile()) &&
						(file.canRead())) {
					DataController.getInstance().open(fileName);
				}
			} else {
				DataController.getInstance().createNewProject();
			}
		} else {
			DataController.getInstance().createNewProject();
		}
	}
	
	public boolean preShutdown() {
		Log.getInstance(ApplicationWorkbenchAdvisor.class).debug("Shutting down."); //$NON-NLS-1$
		
		// Save the project if asked, the project is modified and has a filename.
		if ((Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.AUTOSAVE_ON_EXIT)) &&
				(DataController.getInstance().getProject().isModified()) &&
				(DataController.getInstance().hasFileName())) {
			DataController.getInstance().save();
		} else {	
			// Otherwise, ask user if needed.
			new UnsavedModifiedCheckerAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()).run();
		}
		
		Controller.getInstance().setApplicationStopping(true);
		EventController.getInstance().setEventPropagationEnabled(false);
		
		return true;
	}
	
	public String getMainPreferencePageId() {
		return "toedo-core.Preferences.General"; //$NON-NLS-1$
	}
}
